package edu.zut.test;

import edu.zut.main.pojo.SysUser;
import edu.zut.main.util.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCtest03 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection = DBUtils.getconnection();
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        System.out.println("---请选择要进行的操作---");
        System.out.println("|*******1.查询*******|");
        System.out.println("|*******2.增加*******|");
        System.out.println("|*******3.删除*******|");
        System.out.println("|*******4.改动*******|");
        System.out.println("---------------------");
        num = scanner.nextInt();
        if(num == 1) {
            //编写sql  (?占位符   后期会进行动态数据的替换)
            String sqlc = "select * from sys_user where user_name=? and user_password=?";
            System.out.println("---查询数据库成员操作---");
            System.out.println("请输入用户姓名：");
            Object input_name = scanner.next();
            System.out.println("请输入用户密码：");
            Object input_password = scanner.next();
            ResultSet rsc = DBUtils.query(sqlc, input_name, input_password);
            //处理结果
            SysUser sysUser = null;
            while (rsc.next()) {
                //解析每一行获取的字段数据
                int user_id = rsc.getInt("user_id");
                String user_code = rsc.getString("user_code");
                String user_name = rsc.getString("user_name");
                String user_password = rsc.getString("user_password");
                sysUser = new SysUser(user_id, user_code, user_name, user_password);
            }
            System.out.println(sysUser);
            DBUtils.free();
        }else if(num ==2) {
            //增
            String sqlz = "insert into sys_user values (4,'m004','小何','4567')";
            DBUtils.add(sqlz);
            DBUtils.free();
        }else if(num ==3){
            //删
            String sqls = "delete from sys_user where user_name ='小何'";
            DBUtils.delete(sqls);
            DBUtils.free();
        }else if(num ==4){
            //改
            String sqlg = "update sys_user set user_password=1234 where user_name ='小李'";
            DBUtils.update(sqlg);
            DBUtils.free();
        }else{
            System.out.println("输入错误！！");
        }


        /**
         *实际中，每次都要按照这个步骤写？ 封装工具类，简化操作步骤
         *DBUtils： 驱动的加载、连接的获取、资源的关闭、查询、增删改
         */
    }
}
